//
//  SearchPostViewController.m
//  礼物go
//
//  Created by dllo on 16/5/31.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchPostViewController.h"

@interface SearchPostViewController ()

@end

@implementation SearchPostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"";
    
    self.navigationItem.leftBarButtonItem = nil;
    
    self.navigationItem.rightBarButtonItem = nil;
    
    self.postCTableView.frame = CGRectMake(0, 40, SCREENWIDTH, self.view.frame.size.height - 66);
    
    
    
    
    // Do any additional setup after loading the view.
}

-(void)setSearchPostString:(NSString *)searchPostString{
    _searchPostString = searchPostString;
    [self getSelectList:0];
    
}

-(void)getSelectList:(NSUInteger)nextCursor{
   
    AFNetworkReachabilityManager *netWorkManger = [AFNetworkReachabilityManager sharedManager];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/post?keyword=%@&limit=20&offset=%ld&sort=",_searchPostString,nextCursor * 20];
   // NSLog(@"%@",urlStr);
    [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
        // NSLog(@"")
        [netWorkManger stopMonitoring];
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.dataArray removeAllObjects];
        }
        
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSArray *postsArray = [dataDic objectForKey:@"posts"];
        //NSLog(@"%@",dataDic);
        for (NSDictionary *dic in postsArray) {
            SelectContentModel *selectModel = [[SelectContentModel alloc] init];
            [selectModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:selectModel];
        }
         [self.postCTableView.mj_header endRefreshing];
        [self.postCTableView.mj_footer endRefreshing];
        
        [self.postCTableView reloadData];
        
        
    } error:^(NSError *error) {
        
        
    }];

    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:postCCell];
    SelectContentModel *select = [self.dataArray objectAtIndex:indexPath.row];
    cell.selectContent = select;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
     //设置字体自高亮
    NSString *resultStr = select.title;
    //获取输入框的内容
    NSString *searchStr = _searchPostString;
   // NSLog(@"%@",_searchPostString);
    
 NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:resultStr];
    
    //范围
    NSRange range = [resultStr rangeOfString:searchStr];
    //设置字体颜色
  [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    // NSForegroundColorAttributeName 颜色
    // NSFontAttributeName 字体
    // NSBackgroundColorAttributeName 背景颜色

    cell.titleLabel.attributedText = attributeStr;
    
    return cell;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
